package com.zxn.tablayoutsamples.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.zxn.tablayout.ScrollTabLayout;
import com.zxn.tablayout.listener.CustomTabEntity;
import com.zxn.tablayout.listener.OnTabDelListener;
import com.zxn.tablayout.listener.OnTabSelectListener;
import com.zxn.tablayoutsamples.R;
import com.zxn.tablayoutsamples.entity.IConstantData;
import com.zxn.tablayoutsamples.entity.TabEntity;
import com.zxn.tablayoutsamples.utils.ViewFindUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 可以滚动的Tab、
 */
public class ScrollTabActivity extends AppCompatActivity implements OnTabSelectListener {

    private ScrollTabLayout scrollTabLayout_0;
    private ScrollTabLayout delScrollTabLayout;

    public static void jumpTo(Context context) {
        Intent intent = new Intent(context, ScrollTabActivity.class);
        context.startActivity(intent);
    }

    private final Context mContext = this;
    private final List<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sliding_simple_tab);
        onInitTypeTabLayout();
        onInitTabLayout();
        onSetTabData(IConstantData.TITLE_FRUIT);

        findViewById(R.id.btnScroll).setOnClickListener(v -> {
            int lastTab = scrollTabLayout_0.getTabCount() - 1;
            scrollTabLayout_0.setCurrentTab(lastTab);
        });

        onInitDelTabLayout();
    }

    private final List<CustomTabEntity> tabDataDrinks = new ArrayList<>();

    private void onInitDelTabLayout() {
        delScrollTabLayout = findViewById(R.id.delScrollTabLayout);
        tabDataDrinks.clear();
        for (String record : IConstantData.TITLE_DRINKS) {
            tabDataDrinks.add(new TabEntity(record));
        }
        delScrollTabLayout.setTabData(tabDataDrinks);
        delScrollTabLayout.showDelView(true);
        delScrollTabLayout.setOnTabDelListener(position -> {
            Toast.makeText(this, "setOnTabDelListener:" + position, Toast.LENGTH_SHORT).show();
            tabDataDrinks.remove(position);
            delScrollTabLayout.setTabData(tabDataDrinks);
            delScrollTabLayout.showDelView(true);
            delScrollTabLayout.setCurrentTab(0);
        });
    }

    private void onInitTypeTabLayout() {
        ScrollTabLayout typeScrollTab = findViewById(R.id.typeScrollTab);
        List<CustomTabEntity> tabData = new ArrayList<>();
        for (String record : IConstantData.TITLE_TYPE) {
            tabData.add(new TabEntity(record));
        }
        typeScrollTab.setTabData(tabData);
        typeScrollTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                onSetTabData(IConstantData.TITLE_LIST[position]);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private void onInitTabLayout() {
        View decorView = getWindow().getDecorView();
        scrollTabLayout_0 = ViewFindUtils.find(decorView, R.id.scrollTabLayout_0);
        scrollTabLayout_0.showDelView(true);
        scrollTabLayout_0.setOnTabDelListener(position -> Toast.makeText(this, "setOnTabDelListener:" + position, Toast.LENGTH_SHORT).show());
        scrollTabLayout_0.setOnTabSelectListener(this);
    }

    private void onSetTabData(String[] titles) {
        mTabEntities.clear();
        for (String tab : titles) {
            mTabEntities.add(new TabEntity(tab));
        }
        scrollTabLayout_0.setTabData(mTabEntities);
        scrollTabLayout_0.setCurrentTab(scrollTabLayout_0.getTabCount() - 1);

    }

    @Override
    public void onTabSelect(int position) {
        Toast.makeText(mContext, "onTabSelect&position--->" + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTabReselect(int position) {
        Toast.makeText(mContext, "onTabReselect&position--->" + position, Toast.LENGTH_SHORT).show();
    }

}
